import { Effect, Reducer } from 'umi';
import { IExamType, ISubject, IQuestionsType, IQuestions, IgetQuestionsTypeList } from '@/interface/question'
import { getExamType, getSubject, getQuestionsType, getquestions, getQuestionsTypeList, addexamquestions,addinsertQuestionsType,getquestionscondition,editQuestion } from '@/services/question'

export interface QuestionModulState {
    examType: IExamType[],
    subject: ISubject[],
    questionsType: IQuestionsType[],
    getquestions: IQuestions[],
    getQuestionsTypeList: IgetQuestionsTypeList[],
}

export interface QuestionModulType {
    namespace: 'question',
    state: QuestionModulState,
    effects: {
        getExamType: Effect;
        getSubject: Effect;
        getQuestionsType: Effect;
        getquestions: Effect,
        getQuestionsTypeList: Effect
        addexamquestions: Effect
        addinsertQuestionsType:Effect
        getquestionscondition:Effect
        editQuestionList: Effect  //编辑试题
    };
    reducers: {
        save: Reducer<QuestionModulState>;
    }
}

const questionModel: QuestionModulType = {
    namespace: 'question',
    state: {
        examType: [],
        subject: [],
        questionsType: [],
        getquestions: [],
        getQuestionsTypeList: []
    },
    effects: {
        //获取考试类型
        *getExamType({ payload }, { call, put }) {
            let result = yield call(getExamType)
            if (result.code === 1) {
                yield put({
                    type: 'save',
                    payload: { examType: result.data }
                })
            }
        },
        *getSubject({ payload }, { call, put }) {
            let result = yield call(getSubject)
            if (result.code === 1) {
                yield put({
                    type: 'save',
                    payload: { subject: result.data }
                })
            }
        },
        *getQuestionsType({ payload }, { call, put }) {
            let result = yield call(getQuestionsType)
            if (result.code === 1) {
                yield put({
                    type: 'save',
                    payload: { questionsType: result.data }
                })
            }
        },
        //获取查看试题列表数据
        *getquestions({ payload }, { call, put }) {
            let result = yield call(getquestions)
            if (result.code === 1) {
                yield put({
                    type: 'save',
                    payload: { getquestions: result.data }
                })
            }
        },
        //获取试题分类列表
        *getQuestionsTypeList({ payload }, { call, put }) {
            let result = yield call(getQuestionsTypeList)
            if (result.code === 1) {
                yield put({
                    type: 'save',
                    payload: { getQuestionsTypeList: result.data }

                })
            }
        },
        //添加试题
        *addexamquestions({ payload }, { call, put }) {
            let result=yield call(addexamquestions,payload)
            return result
        },
        //添加试题分类
        *addinsertQuestionsType({payload},{call,put}){
            let result=yield call(addinsertQuestionsType,payload)
               if(result.code===1){
                   yield put({
                        type:"getQuestionsTypeList",
                   })
               }
            return result
        },
        //查询试题
        *getquestionscondition({payload},{call,put}){
            console.log(5)
             let result=yield call(getquestionscondition,payload)
             if(result.code===1){
                 yield put({
                      type:"save",
                      payload:{getquestions:result.data}
                 })
             }
             console.log(result,'查询试题分论坛')
        },
        //编辑试题
        *editQuestionList({ payload }, { call, put }){
            console.log(payload)
            let data = yield call(editQuestion,payload)
            console.log(data)
            if(data.code===1){
            yield put({
                type : 'getQuestionList'
            })
            }
        },

    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    }
}
export default questionModel;